<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改后台配置')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-set-edit" th:object="${ydzSet}">
            <input name="id" th:field="*{id}" id="id" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">400 电话：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" id="name" class="form-control" type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label label-size">通用二维码：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="commonCard" id="commonCard" th:field="*{commonCard}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="commonCard" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label label-size">临床招募二维码:</label>
                <div class="col-sm-8">
                    <input type="hidden" name="lcCard" id="lcCard" th:field="*{lcCard}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="lcCard" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">企业微信链接：</label>
                <div class="col-sm-8">
                    <input name="qwUrl" id="qwUrl" class="form-control" th:field="*{qwUrl}" >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">临床招募企微链接：</label>
                <div class="col-sm-8">
                    <input name="lcUrl" id="lcUrl" class="form-control" th:field="*{lcUrl}" >
                </div>
            </div>
        </form>
        <div style="text-align: center">
            <a class="btn btn-newadd" onclick="submitHandler()">
                保存
            </a>
<!--            <a class="btn btn-newadd" onclick="returnPage()">-->
<!--                返回-->
<!--            </a>-->
        </div>

    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:inline="javascript">
        var prefix = ctx + "ydz/set";
        $("#form-set-edit").validate({
            focusCleanup: true
        });
        function saveSet(url, data, callback) {
            var config = {
                url: url,
                type: "post",
                dataType: "json",
                data: data,
                headers: {
                    'Content-Type': 'application/json;charset=utf8'
                },
                success: function (res) {
                   if(res.code===0){
                       $.modal.msgSuccess("保存成功！");
                   }else{
                        $.modal.alertError("保存失败，请联系管理员！");
                   }
                }
            };
            $.ajax(config)
        }

        function submitHandler() {
            var data = {}
            data.id = $('#id').val();
            data.name = $('#name').val();
            data.commonCard = $('#commonCard').val();
            data.lcCard = $('#lcCard').val();
            data.qwUrl = $('#qwUrl').val();
            data.lcUrl = $('#lcUrl').val();
            var value = JSON.stringify(data)
            if ($.validate.form()) {
                //$.operate.saveSet(prefix + "/edit", $('#form-set-edit').serialize());
                saveSet(prefix + "/edit", value);

            }
        }
        function returnPage() {
            window.close();
        }

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            if (val==""){
                $(".file-upload").fileinput({
                    uploadUrl: ctx + 'common/upload',
                    maxFileCount: 1,
                    autoReplace: true
                }).on('fileuploaded', function (event, data, previewId, index) {
                    console.log(data)
                    $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
                }).on('fileremoved', function (event, id, index) {
                    $("input[name='" + event.currentTarget.id + "']").val('')
                })
            }else{
                $(this).fileinput({
                    'uploadUrl': ctx + 'common/upload',
                    initialPreviewAsData: true,
                    initialPreview: [val],
                    maxFileCount: 1,
                    autoReplace: true
                }).on('fileuploaded', function (event, data, previewId, index) {
                    console.log(data)
                    $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
                }).on('fileremoved', function (event, id, index) {
                    $("input[name='" + event.currentTarget.id + "']").val('')
                })
                $(this).fileinput('_initFileActions');
            }
        });
    </script>
</body>
</html>